package com.leonyr.smartipaddemo.crop;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leonyr.smartipaddemo.R;
import com.leonyr.smartipaddemo.view.SeekBarWidthEdit;

public class MoreSetFragment extends Fragment implements View.OnClickListener{
    public static final String CAMERA_BIRGHTNESS = "CAMERA_BIRGHTNESS";
    public static final String CAMERA_CONTRAST  = "CAMERA_CONTRAST ";
    public static final String CAMERA_GAMMA = "CAMERA_GAMMA";
    public static final String CAMERA_WB_TEMP = "CAMERA_WB_TEMP";
    public static final String CAMERA_SATURATION = "CAMERA_SATURATION";
    public static final String CAMERA_HUE = "CAMERA_HUE";
    public static final String CAMERA_SHARPNESS = "CAMERA_SHARPNESS";
    public static final String CAMERA_BACKLIGHT = "CAMERA_BACKLIGHT";

    private TextView tvBack;
    private SeekBarWidthEdit seekbar_brightness;//亮度
    private SeekBarWidthEdit seekbar_contrast;//对比度
    private SeekBarWidthEdit seekbar_gama;//伽马
    private SeekBarWidthEdit seekbar_wb_temp;//伽马
    private SeekBarWidthEdit seekbar_satura;//饱和度
    private SeekBarWidthEdit seekbar_hue;//色调
    private SeekBarWidthEdit seekbar_sharpness;//锐利度
    private SeekBarWidthEdit seekbar_backlight;//背光补偿
    private OnSeekBarProgressChangedListener listener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (OnSeekBarProgressChangedListener) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fm_more_set,container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        Bundle data = getArguments();
        int brightness = data.getInt(CAMERA_BIRGHTNESS);
        int contrast = data.getInt(CAMERA_CONTRAST);
        int gama = data.getInt(CAMERA_GAMMA);
        int wbTemp = data.getInt(CAMERA_WB_TEMP);
        int saturation = data.getInt(CAMERA_SATURATION);
        int hue = data.getInt(CAMERA_HUE);
        int sharpness = data.getInt(CAMERA_SHARPNESS);
        int backlight = data.getInt(CAMERA_BACKLIGHT);

        tvBack = view.findViewById(R.id.tv_back);
        tvBack.setOnClickListener(this);
        seekbar_brightness = view.findViewById(R.id.seekbar_brightness);
        seekbar_brightness.setProgress(brightness);
        seekbar_brightness.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_brightness));
        seekbar_brightness.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_brightness));
        seekbar_contrast = view.findViewById(R.id.seekbar_contrast);
        seekbar_contrast.setProgress(contrast);
        seekbar_contrast.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_contrast));
        seekbar_contrast.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_contrast));
        seekbar_gama = view.findViewById(R.id.seekbar_gama);
        seekbar_gama.setProgress(gama);
        seekbar_gama.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_gama));
        seekbar_gama.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_gama));
        seekbar_wb_temp = view.findViewById(R.id.seekbar_wb_temp);
        seekbar_wb_temp.setProgress(wbTemp);
        seekbar_wb_temp.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_wb_temp));
        seekbar_wb_temp.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_wb_temp));
        seekbar_satura = view.findViewById(R.id.seekbar_satura);
        seekbar_satura.setProgress(saturation);
        seekbar_satura.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_satura));
        seekbar_satura.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_satura));
        seekbar_hue = view.findViewById(R.id.seekbar_hue);
        seekbar_hue.setProgress(hue);
        seekbar_hue.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_hue));
        seekbar_hue.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_hue));
        seekbar_sharpness = view.findViewById(R.id.seekbar_sharpness);
        seekbar_sharpness.setProgress(sharpness);
        seekbar_sharpness.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_sharpness));
        seekbar_sharpness.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_sharpness));
        seekbar_backlight = view.findViewById(R.id.seekbar_backlight);
        seekbar_backlight.setProgress(backlight);
        seekbar_backlight.setOnSeekBarWidthEditProgressChanged(new MySeekbarWidthEditProgressChangedListener(R.id.seekbar_backlight));
        seekbar_backlight.setOnSeekBarWidthEditTextChanged(new MySeekbarWidthEditTextChangedListener(R.id.seekbar_backlight));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_back:
                getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
                break;
        }
    }

    public interface OnSeekBarProgressChangedListener{
        void onBrightnessChange(int value);
        void onContrastChange(int value);
        void onGamaChange(int value);
        void onWbTempChange(int value);
        void onSaturaChange(int value);
        void onHueChange(int value);
        void onSharpnessChange(int value);
        void onBacklightChange(int value);
    }

    public static MoreSetFragment newInstance(int brightness,int contrast,int gama,int wbTemp,int saturation,
                                              int hue,int sharpness,int backlight){
        MoreSetFragment fragment = new MoreSetFragment();
        Bundle data = new Bundle();
        data.putInt(CAMERA_BIRGHTNESS,brightness);
        data.putInt(CAMERA_CONTRAST,contrast);
        data.putInt(CAMERA_GAMMA,gama);
        data.putInt(CAMERA_WB_TEMP,wbTemp);
        data.putInt(CAMERA_SATURATION,saturation);
        data.putInt(CAMERA_HUE,hue);
        data.putInt(CAMERA_SHARPNESS,sharpness);
        data.putInt(CAMERA_BACKLIGHT,backlight);
        fragment.setArguments(data);
        return fragment;
    }

    public class MySeekbarWidthEditProgressChangedListener implements SeekBarWidthEdit.OnSeekBarWidthEditProgressChanged{
        private int id;

        public MySeekbarWidthEditProgressChangedListener(int id) {
            this.id = id;
        }

        @Override
        public void onProgressChanged(int progress) {
            switch (id){
                case R.id.seekbar_brightness:
                    listener.onBrightnessChange(progress);
                    break;
                case R.id.seekbar_contrast:
                    listener.onContrastChange(progress);
                    break;
                case R.id.seekbar_gama:
                    listener.onGamaChange(progress);
                    break;
                case R.id.seekbar_wb_temp:
                    listener.onWbTempChange(progress);
                    break;
                case R.id.seekbar_satura:
                    listener.onSaturaChange(progress);
                    break;
                case R.id.seekbar_hue:
                    listener.onHueChange(progress);
                    break;
                case R.id.seekbar_sharpness:
                    listener.onSharpnessChange(progress);
                    break;
                case R.id.seekbar_backlight:
                    listener.onBacklightChange(progress);
                    break;
            }
        }
    }

    public class MySeekbarWidthEditTextChangedListener implements SeekBarWidthEdit.OnSeekBarWidthEditTextChanged{
        private int id;

        public MySeekbarWidthEditTextChangedListener(int id) {
            this.id = id;
        }

        @Override
        public void onTextChanged(String text) {
            int progress = Integer.parseInt(text);
            switch (id){
                case R.id.seekbar_brightness:
                    listener.onBrightnessChange(progress);
                    break;
                case R.id.seekbar_contrast:
                    listener.onContrastChange(progress);
                    break;
                case R.id.seekbar_gama:
                    listener.onGamaChange(progress);
                    break;
                case R.id.seekbar_wb_temp:
                    listener.onWbTempChange(progress);
                    break;
                case R.id.seekbar_satura:
                    listener.onSaturaChange(progress);
                    break;
                case R.id.seekbar_hue:
                    listener.onHueChange(progress);
                    break;
                case R.id.seekbar_sharpness:
                    listener.onSharpnessChange(progress);
                    break;
                case R.id.seekbar_backlight:
                    listener.onBacklightChange(progress);
                    break;
            }
        }
    }
}
